package com.fdq.dianping.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdq.dianping.entity.Result;
import com.fdq.dianping.entity.Shop;
import com.fdq.dianping.service.IShopService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fdq
 * @since 2024-12-29
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Resource
    public IShopService  shopService;
    /*
    * 按id查询商户
    * */
    @GetMapping("/{id}")
    public Result queryShopById(@PathVariable("id") Long id, HttpServletResponse response){
        return shopService.queryById(id, response);
    }

    /*
    * 按id更新商户
    * */
    @PutMapping("/update")
    public Result update(@RequestBody Shop shop){
        return shopService.update(shop);
    }

    /*
    * 按名查询商户
    * */
    @GetMapping("/name")
    public Result queryByName(String name){
        return shopService.queryByName(name);
    }

    /*
    * 按商户类型查询商户（按距离排序、按评分排序)
    * */
    @GetMapping("/of/type")
    public Result queryShopByType(
            @RequestParam("typeId") Integer typeId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,       //分页
            @RequestParam(value = "x",required = false) Double x,                                                //坐标
            @RequestParam(value = "y",required = false) Double y){
            return shopService.queryShopByType(typeId,current,x,y);
    }

    /**
     * 按距离查询商户
     */
    @GetMapping("/of/distance")
    public Result queryShopByDistance(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "x") Double x,                                                //坐标
            @RequestParam(value = "y") Double y
    ){
        return shopService.queryShopByDistance(current,x,y);
    }


    /**
     * 注册商户
     */
    @PostMapping("/register")
    public Result insertShop(@RequestBody Shop shop){
        boolean save = shopService.save(shop);
        if(save){
            return Result.success();
        }else {
            return Result.error("注册商户失败");
        }
    }


    /**
     * 按商户负责人id查询
     */
    @GetMapping("/master/{id}")
    public Result queryByMasterId(@PathVariable("id") Long id){
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("master_id",id);
        List<Shop> shops = shopService.list(queryWrapper);
        return Result.success(shops);
    }


}
